package yunquan.wang.base.spring.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import yunquan.wang.base.web.ajax.AjaxDone;

public class CustomerAccessDeniedHandler implements AccessDeniedHandler{
	@Override
	public void handle(HttpServletRequest request,
			HttpServletResponse response,
			AccessDeniedException accessDeniedException) throws IOException,
			ServletException {
		MappingJacksonHttpMessageConverter convert=new MappingJacksonHttpMessageConverter();
 AjaxDone done=new AjaxDone();
 ServerHttpResponse responsSpring= new ServletServerHttpResponse(response);
 done.setStatusCode(AjaxDone.STATUS_ERROR);
 done.setMessage(accessDeniedException.getMessage());
  ((HttpMessageConverter ) convert).write(done, MediaType.parseMediaType(request.getHeader("Accept")), responsSpring);
	}
}
